import Vue from "vue";
import Vuex from "vuex";
//导入本地存储
import { getItem, setItem, removeItem } from "@/utils/storage";

Vue.use(Vuex);

const TOKEN = "USER-TOKEN";
export default new Vuex.Store({
  state: {
    // user: JSON.parse(window.localStorage.getItem(TOKEN))
    user: getItem(TOKEN)
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
      //本地存储
      // window.localStorage.setItem(TOKEN, JSON.stringify(user));
      setItem(TOKEN, user);
    },
    //清除token  退出登录时
    removeUser(state) {
      /* 点击退出登录 
      1-清除state中的user
      2-清除本地缓存
      */
      state.user = null;
      removeItem(TOKEN);
    }
  },
  actions: {},
  modules: {}
});
